package com.aos.erp.manager.service.sys.impl;

import com.aos.erp.common.exception.BizException;
import com.aos.erp.manager.enums.IsDel;
import com.aos.erp.manager.mapper.sys.DepartmentMapper;
import com.aos.erp.manager.mapper.sys.PositionMapper;
import com.aos.erp.manager.model.sys.Department;
import com.aos.erp.manager.model.sys.Position;
import com.aos.erp.manager.model.sys.dto.PositionDto;
import com.aos.erp.manager.model.sys.vo.PositionVo;
import com.aos.erp.manager.service.sys.Positionservice;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: erp
 * @description:岗位service
 * @author: tanzhiyu
 * @create: 2018-11-05 18:28
 **/
@Service
public class PositionserviceImpl extends ServiceImpl<PositionMapper, Position> implements Positionservice {
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private DepartmentMapper departmentMapper;

    public void addBean(PositionVo positionVo) throws Exception {
        Position position = new Position();
        position.setCreated(new Date());
        BeanUtils.copyProperties(positionVo, position);
        Map<String, Object> params = new HashMap<>();
        params.put("depCode", positionVo.getDepCode());
        Department dep = departmentMapper.findByParams(params);
        if (dep == null) {
            throw new BizException("部门组织code错误");
        }
        position.setDepId(dep.getId());
        position.setIsDel(Integer.valueOf(IsDel.normal.getValue().toString()));
        positionMapper.insert(position);
    }

    /**
     * 新增--驾校
     *
     * @param positionVo
     * @throws Exception
     */
    public void addBeanSchool(PositionVo positionVo) throws Exception {
        Position position = new Position();
        position.setCreated(new Date());
        BeanUtils.copyProperties(positionVo, position);
        if(StringUtils.isNotEmpty(positionVo.getDepCodeB())){
            positionVo.setDepCode(positionVo.getDepCodeB());
        }else if(StringUtils.isNotEmpty(positionVo.getDepCodeF())){
            positionVo.setDepCode(positionVo.getDepCodeF());
        }

        Map<String, Object> params = new HashMap<>();
        params.put("depCode", positionVo.getDepCode());
        Department dep = departmentMapper.findByParams(params);
        if (dep == null) {
            throw new BizException("部门组织code错误");
        }
        position.setDepId(dep.getId());
        position.setIsDel(Integer.valueOf(IsDel.normal.getValue().toString()));
        positionMapper.insert(position);
    }

    /**
     * 修改
     *
     * @param positionVo
     * @throws Exception
     */
    public void updateBean(PositionVo positionVo) throws Exception {
        Position position = new Position();
        position.setCreated(new Date());
        BeanUtils.copyProperties(positionVo, position);
        Map<String, Object> params = new HashMap<>();
        params.put("depCode", positionVo.getDepCode());
        Department dep = departmentMapper.findByParams(params);
        if (dep == null) {
            throw new BizException("部门组织code错误");
        }
        position.setIsDel(Integer.valueOf(IsDel.normal.getValue().toString()));
        position.setDepId(dep.getId());
        position.setUpdated(new Date());
        position.setId(Long.valueOf(positionVo.getId()));
        this.updateById(position);
    }

    /**
     * 删除岗位
     *
     * @param id
     * @return
     */
    public int isDel(String id) throws Exception {
        Position position = new Position();
        position.setId(Long.valueOf(id));
        position.setIsDel(Integer.valueOf(IsDel.del.getValue().toString()));
        int i = baseMapper.updateObject(position);
        return i;
    }

    /**
     * 根据岗位id查询
     *
     * @param id
     * @return
     * @throws Exception
     */
    public Position selectById(String id) throws Exception {
        return baseMapper.selectById(Long.valueOf(id));
    }

    public List<PositionDto> findByParams(Map<String, Object> map) {
        return baseMapper.findByParams(map);
    }

    public PositionDto selectByParams(Map<String, Object> map) {
        List<PositionDto> list = baseMapper.findByParams(map);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public IPage<PositionDto> queryByPage(Integer pageIndex, Integer pageSize, String depCode, String code, String name, String depType) {
        Page<PositionDto> pageBean = new Page<PositionDto>(pageIndex, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("depCode", depCode);
        map.put("code", code);
        map.put("name", name);
        map.put("isDel", IsDel.normal.getValue().toString());
        map.put("depType", depType);
        map.put("pageBean", pageBean);
        List<PositionDto> list = positionMapper.findByParams(map);
        IPage<PositionDto> page = pageBean;
        page.setRecords(list);
        return page;
    }
}
